import { EAreaID, EPokerState } from "../constant/GameEnum";
import PokerVo from "./PokerVo";

export default class PokerGroupVo {
  private _pokers: PokerVo[] = [];

  private _areaID: EAreaID = EAreaID.CLOSE;
  constructor(areaID: EAreaID = EAreaID.CLOSE) {
    this._pokers = [];
    this._areaID = areaID;
  }
  public get pokers(): PokerVo[] {
    return this._pokers;
  }

  public get areaID(): number {
    return this._areaID;
  }
  public get topPoker(): PokerVo {
    return this.pokers[this.pokers.length - 1];
  }
  public get Length(): number {
    return this._pokers.length;
  }
  public get havePoker(): boolean {
    return this.Length > 0;
  }

  public addPoker(poker: PokerVo) {
    this._pokers.push(poker);
    poker.parent = this;
  }
  public delTopPoker(num: number = 1): void {
    this._pokers.length = this._pokers.length - num;
  }
  public cleanPoker(): void {
    this._pokers.length = 0;
  }

  public getPokerByIndex(index: number): PokerVo {
    if (this.havePoker) {
      let i = index >= 0 ? index : this.pokers.length + index;
      if (i < this.pokers.length) {
        return this.pokers[i];
      } else {
        return null;
      }
    }
    return null;
  }

  public getIndexByGroup(poker: PokerVo): number {
    return this._pokers.indexOf(poker);
  }
  public get rootOpenPoker(): PokerVo {
    for (let i = 0; i < this.pokers.length; i++) {
      if (this.pokers[i].state == EPokerState.OPEN) {
        if (this.pokers[i - 1]) {
          return this.pokers[i];
        }
        return null;
      }
    }
    return null;
  }
  public get rootNoFlipOpenPoker(): PokerVo {
    for (let i = 0; i < this.pokers.length; i++) {
      if (this.pokers[i].state == EPokerState.OPEN) {
        return this.pokers[i];
      }
    }
    return null;
  }
}
